package test

import config.MyFile

object getSeqs extends MyFile {

  def main(args: Array[String]): Unit = {
   val lines =  "H:\\南农油茶数据库\\2024_6_12\\GWHERAW00000000.Protein.faa/GWHERAW00000000.Protein.faa".readFileToString.split(">").tail.map { x =>
      val line = x.split("\n")
      line.head.split("\t").find(_.startsWith("Gene")).get.trim.drop(5)  -> line.tail.mkString.split('.').mkString
    }.groupBy(_._1).map{x=>
     ">" + x._1 + "\n" + x._2.maxBy(_._2.length)._2
   }.mkString("\n")

    "H:\\南农油茶数据库\\2024_6_12/Camellia_crapnelliana.pep".writeStringToFile(lines)

  }


  def get = {

    val path = "H:\\南农油茶数据库\\南荣油茶基因组数据\\CON_genome_v1.0_all.genes.gff"

    val annos = s"$path/annotation.table.xls".readLines.tail.map { x =>
      val line = x.split("\t")
      val geneid = line.head.split('-').dropRight(2).mkString("-")

      geneid -> line
    }.groupBy(_._1).map { x =>
      x._1 -> x._2.map(_._2).maxBy(_(1).toInt)
    }

    val mrnaToGene = annos.map(x => x._2.head -> x._1)


    val pep = "I:\\camellia_oleifera_abel_db\\blastData/cds.fa".readFileToString.split(">").tail.map { x =>
      val line = x.split("\n")
      val geneid = mrnaToGene.getOrElse(line.head.split(" ").head, "None")
      geneid -> line.tail.mkString
    }.filter(_._1 != "None").map { x =>
      ">" + x._1 + "\n" + x._2
    }.mkString("\n")


    "I:\\camellia_oleifera_abel_db\\blastData/cds/cds.fa".writeStringToFile(pep)

  }
}
